<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/10 0010
 * Time: 下午 3:04
 */

namespace Admin\Model;


use Think\Model;
use Think\Page;

class ArticleCategoryModel extends Model{
    /**
     * 获取分页
     * @return array $rows
     * @param array $cond
     */
    public function getPageResult($cond = array(),$page=1){
        $cond['status'] = array('gt',-1);
        //获取总条数
        $count = $this->where($cond)->count();
        //设置显示条数
        $size = C('PAGE_SIZE');
        //实例化page传参
        $page_obj = new Page($count,$size);
        //分页设置样式
        $page_obj->setConfig('theme',C('PAGE_THEME'));
        //显示html
        $page_html = $page_obj->show();
        $rows = $this->where($cond)->page($page,$size)->order('sort')->select();
        return array(
            'page_html'=>$page_html,
            'rows'=> $rows,
        );
    }

    /**
     * 自动验证规则
     * @var array
     */
    protected $_validate = array(
        array('name', 'require', '分类名不能为空'),
        array('name', '', '分类名已存在', self::EXISTS_VALIDATE, 'unique'),
        array('sort', 'number', '排序必须为数字', 2)
    );

    /**
     * 逻辑删除
     * @param $id
     * @return bool
     */
    public function getDel($id) {
        //构建要修改的对象
        $data = array(
            'status' => '-1',
            'name' => array('exp', "CONCAT(name,'_del')"),
        );
        return $this->where("id=$id")->setField($data);
    }


}